<?php
/*
Widget Name: LatestPosts
Widget URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: LatestPosts to Archive and Category.
Version: 1.0
Author: Bertrand SCARPA
Author URI: http://www.carbonpaper.eu
*/


/**
 * Display Archives for Quick Access widget.
 *
 * @param array $args Widget arguments.
 */

require_once( ABSPATH . WPINC . '/class.wp-multiwidget.php');

class LatestPosts extends MultiWidget
{
  function LatestPosts()
  {
    $this->MultiWidget(
        'latestposts', // id_base
        'Carbon Paper :: Articles récents', // name
        array('description'=>__('The most recent posts on your site'))
      );
  }


  // Echo the actual widget content. 
  function widget($args, $instance){
		global $before_widget;
		global $after_widget;
		global $post;
		extract($args); 
		$r = new WP_Query(array('post_type' => $instance['post_type'],'showposts' => $instance['latestposts_number'], 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1));
	    $my_thumb = get_post_meta($post->ID,'my_thumb01',1) . get_post_meta($post->ID,'my_thumb02',1) . get_post_meta($post->ID,'my_thumb03',1) ;
	    $my_layout = get_post_meta($post->ID,'my_layout',1);
	    if ($r->have_posts() && $my_thumb == '' || ($my_thumb != '' && $my_layout == 'portrait')) :
	?>
		<?php echo $before_widget; ?>
		<h3><?php echo $instance['latestposts_title']?></h3>
		<?php  while ($r->have_posts()) : $r->the_post(); ?>
			<p class="date" style="text-transform:capitalize"><?php the_time('j M Y') ?></p>
			<h4><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></h4>
			<p><?php if (get_the_excerpt() != ''): echo get_the_excerpt(); else: echo wp_trim_excerpt('',16); endif;?></p>
		<?php endwhile; ?>
		<?php echo $after_widget; ?>
	<?php
		wp_reset_query();  // Restore global post data stomped by the_post().
		endif;
  }


  // Update a particular instance.
  // This function should check that $new_instance is set correctly.
  // The newly calculated value of $instance should be returned.
  function control_update($new_instance, $old_instance)
  {
    if( !isset($new_instance['latestposts_title']) ) // user clicked cancel
        return false;
    $instance = $old_instance;
    $instance['latestposts_title'] = wp_specialchars( $new_instance['latestposts_title'] );
   	$instance['post_type'] = wp_specialchars( $new_instance['post_type'] );
	if ( !$new_instance['latestposts_number'] = (int) $new_instance['latestposts_number'] )
		$instance['latestposts_number'] = 5;
	else
	    $instance['latestposts_number'] = wp_specialchars( $new_instance['latestposts_number'] );
    return $instance;
  }
	

  // Echo a control form for the current instance.
  // The form has inputs with names like widget-ID_BASE[$number][FIELD_NAME]
  // so that all data for that instance of the widget are stored in one
  // $_POST variable: $_POST['widget-ID_BASE'][$number]
  function control_form($instance)  {
?>
		<p><label for="<?php echo $this->get_field_id('latestposts_title') ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('latestposts_title') ?>" name="<?php echo $this->get_field_name('latestposts_title') ?>" type="text" value="<?php echo htmlspecialchars($instance['latestposts_title'],ENT_QUOTES) ?>"/></label></p>
		<p><label for="<?php echo $this->get_field_id('post_type') ?>"><?php _e('Post Type:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('post_type') ?>" name="<?php echo $this->get_field_name('post_type') ?>" type="text" value="<?php echo htmlspecialchars($instance['post_type'],ENT_QUOTES) ?>"/></label></p>
		<p>
			<label for="<?php echo $this->get_field_id('latestposts_number') ?>"><?php _e('Number of posts to show:'); ?> <input style="width: 25px; text-align: center;" id="<?php echo $this->get_field_id('latestposts_number') ?>" name="<?php echo $this->get_field_name('latestposts_number') ?>" type="text" value="<?php echo htmlspecialchars($instance['latestposts_number'],ENT_QUOTES) ?>" /></label>
			<br />
			<small><?php _e('(at most 15)'); ?></small>
		</p>
		<input type="hidden" id="latestposts-submit" name="latestposts-submit" value="1" />
<?php
  }
} // end class ExampleMultiWidget


// Finally create an object for the widget-type and register it.
$latestposts = new LatestPosts();
add_action( 'widgets_init', array($latestposts,'register') );

?>